package struct;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import struct.enums.CtxValidator;
import util.Util;

import java.util.HashMap;
import java.util.Map;

public class CtxServiceConfig implements ServiceConfig{
    private static final String RULE_FILE_FIELD = "rule_file_content";
    private static final String PATTERN_FILE_FIELD = "pattern_file_content";
    private static final String BFUNC_FILE_FIELD = "bfunc_file_content";
    private static final String MFUNC_FILE_FIELD = "mfunc_file_content";
    private static final String RFUNC_FILE_FIELD = "rfunc_file_content";
    private static final String VALIDATOR_FIELD = "ctx_validator";

    private Map<String, String> config;

    public CtxServiceConfig() {
        config = new HashMap<>();
        config.put(VALIDATOR_FIELD, CtxValidator.PCC_IMD.toString());//默认值
    }

    public CtxServiceConfig(String ruleFile, String patternFile, String bfuncFile, String mfuncFile, String rfuncFile) {
        config = new HashMap<>();
        setCtxResources(ruleFile, patternFile, bfuncFile, mfuncFile, rfuncFile);
    }

    public int size() {
        return config.size();
    }

    public boolean setCtxResources(String ruleFile, String patternFile, String bfuncFile, String mfuncFile, String rfuncFile) {
        String ruleFileContent = ruleFile == null ? null : Util.readFileContent(ruleFile,  "//quota");
        String patternFileContent = patternFile == null ? null : Util.readFileContent(patternFile,  "//quota");
        String bfuncFileContent = bfuncFile == null ? null : Util.readFileContent(bfuncFile,  "//quota");
        String mfuncFileContent = mfuncFile == null ? null : Util.readFileContent(mfuncFile,  "//quota");
        String rfuncFileContent = rfuncFile == null ? null : Util.readFileContent(rfuncFile,  "//quota");

        if (ruleFileContent != null && patternFileContent != null && bfuncFileContent != null) {
            config.put(RULE_FILE_FIELD, ruleFileContent);
            config.put(PATTERN_FILE_FIELD, patternFileContent);
            config.put(BFUNC_FILE_FIELD, bfuncFileContent);
            config.put(MFUNC_FILE_FIELD, mfuncFileContent);
            config.put(RFUNC_FILE_FIELD, rfuncFileContent);
            return true;
        } else {
            return false;
        }
    }

    public boolean setCtxValidator(CtxValidator ctxValidator) {
        config.put(VALIDATOR_FIELD, ctxValidator.toString());
        return true;
    }

    @Override
    public String toJSONString() {
        return toString();
    }

    @Override
    public String toString() {
        JSONObject jo = new JSONObject();
        jo.putAll(config);
        return jo.toJSONString();
    }

    @Override
    public JSONObject toJSONObject() {
        JSONObject jo = new JSONObject();
        jo.putAll(config);
        return jo;
    }

    public static CtxServiceConfig fromJSONString(String json) {
        //TODO:检查是否有效
        CtxServiceConfig config = new CtxServiceConfig();
        JSONObject jo = JSON.parseObject(json);
        for (Map.Entry<String, Object> entry : jo.entrySet()) {
            config.config.put(entry.getKey(), entry.getValue().toString());
        }
        return config;
    }

    public CtxValidator getCtxValidator() {
        if (config.containsKey(VALIDATOR_FIELD)) {
            return CtxValidator.fromString(config.get(VALIDATOR_FIELD));
        } else {
            return null;
        }
    }

    public String getRuleFileContent() {
        return config.getOrDefault(RULE_FILE_FIELD, null);
    }

    public String getPatternFileContent() {
        return config.getOrDefault(PATTERN_FILE_FIELD, null);
    }

    public String getBfuncFileContent() {
        return config.getOrDefault(BFUNC_FILE_FIELD, null);
    }

    public String getMfuncFileContent() {
        return config.getOrDefault(MFUNC_FILE_FIELD, null);
    }

    public String getRfuncFileContent() {
        return config.getOrDefault(RFUNC_FILE_FIELD, null);
    }
}
